package org.jeecg.modules.demo.ipds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.demo.ipds.entity.*;
import org.jeecg.modules.demo.ipds.mapper.ByteListReuseMapper;
import org.jeecg.modules.demo.ipds.service.IBitListReuseService;
import org.jeecg.modules.demo.ipds.service.IByteFrameReuseZuService;
import org.jeecg.modules.demo.ipds.service.IByteListReuseService;
import org.jeecg.modules.demo.ipds.service.ITreeByteListReuseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 字节复用组
 * @Author: jeecg-boot
 * @Date:   2024-03-11
 * @Version: V1.0
 */
@Service
public class ByteListReuseServiceImpl extends ServiceImpl<ByteListReuseMapper, ByteListReuse> implements IByteListReuseService {

    @Autowired
    private IByteListReuseService treeByteListService;


    @Autowired
    private IBitListReuseService bitListService;

    @Autowired
    private IByteFrameReuseZuService byteFrameZuService;
    @Autowired
    private ITreeByteListReuseService treeByteListService2;



    @Override
    public boolean copyAdd(String id, String type, int index)  {
        if (type.equals("1")) {
            //字节
            ByteListReuse byId = treeByteListService.getById(id);
            String byteId = byId.getId();
            byId.setId(null);

            QueryWrapper<BitListReuse> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("byte_id", byteId);
            treeByteListService.save(byId);

            List<BitListReuse> list = bitListService.list(queryWrapper);
            for (BitListReuse bitList : list) {
                bitList.setId(null);
                bitList.setByteId(byId.getId());
                bitListService.save(bitList);
            }

            return true;
        }
        if (type.equals("2")) {
            //子帧组
            ByteListReuse byId = treeByteListService.getById(id);
            String byteId = byId.getId();

            QueryWrapper<ByteFrameReuseZu> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("byte_id", byteId);
            ByteFrameReuseZu byteFrameZu = byteFrameZuService.list(queryWrapper).get(index);
            String zuId = byteFrameZu.getId();

            //新增一个组
            byteFrameZu.setId(null);
            byteFrameZu.setByteId(byteId);
            byteFrameZuService.save(byteFrameZu);

            QueryWrapper<TreeByteListReuse> treeByteList2QueryWrapper = new QueryWrapper<>();
            treeByteList2QueryWrapper.eq("pid", zuId);
            List<TreeByteListReuse> child = treeByteListService2.list(treeByteList2QueryWrapper);

            for (TreeByteListReuse treeByteList2 : child) {
                //新增子帧字节
                String treeByteList2Id = treeByteList2.getId();
                treeByteList2.setId(null);
                treeByteList2.setPid(byteFrameZu.getId());
                treeByteListService2.save(treeByteList2);

                //位
                QueryWrapper<BitListReuse> bitListQueryWrapper = new QueryWrapper<>();
                bitListQueryWrapper.eq("byte_id", treeByteList2Id);
                List<BitListReuse> bitLists = bitListService.list(bitListQueryWrapper);

                //位保存
                if (bitLists != null && bitLists.size() > 0) {
                    for (BitListReuse bitList : bitLists) {
                        bitList.setId(null);
                        bitList.setByteId(treeByteList2.getId());
                        bitListService.save(bitList);
                    }
                }
            }

            //子帧组
            return true;
        }
        if (type.equals("3")) {
            //子帧字节
            TreeByteListReuse treeByteList2 = treeByteListService2.getById(id);
            //新增子帧字节
            String treeByteList2Id = treeByteList2.getId();

            treeByteList2.setId(null);
            treeByteListService2.save(treeByteList2);
            //位
            QueryWrapper<BitListReuse> bitListQueryWrapper = new QueryWrapper<>();
            bitListQueryWrapper.eq("byte_id", treeByteList2Id);
            List<BitListReuse> bitLists = bitListService.list(bitListQueryWrapper);
            //位保存
            if (bitLists != null && bitLists.size() > 0) {
                for (BitListReuse bitList : bitLists) {
                    bitList.setId(null);
                    bitList.setByteId(treeByteList2.getId());
                    bitListService.save(bitList);
                }
            }
            return true;
        }
        if (type.equals("4")) {
            //位
            BitListReuse bitList = bitListService.getById(id);
            bitList.setId(null);
            bitListService.save(bitList);

            return true;
        }


        return false;
    }
}


